{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit FolderForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, MediaFolders, ImgList, PngImageList, pngimage;

type
  TAHMFolderMode = (fmAdd, fmEdit);

  TfrmFolderDetails = class(TForm)
    cbVirtual: TCheckBox;
    edDirectory: TEdit;
    edName: TEdit;
    Label2: TLabel;
    Bevel1: TBevel;
    btBrowse: TButton;
    btFinish: TButton;
    btCancel: TButton;
    Bevel2: TBevel;
    Label3: TLabel;
    Label4: TLabel;
    pbHeader: TPaintBox;
    lbCaption: TLabel;
    lbAction: TLabel;
    procedure FormShow(Sender: TObject);
    procedure btFinishClick(Sender: TObject);
    procedure pbHeaderPaint(Sender: TObject);
    procedure btBrowseClick(Sender: TObject);
    procedure edNameChange(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure edDirectoryExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FMode: TAHMFolderMode;
    FType: TAHMMediaFolderType;
    FFolder: TAHMMediaFolder;
    FHeaderImages: TPngImageList;
    procedure ConfigureForm;
    procedure UpdateGUIState;
    procedure SuggestFolderName(Name: String);
    procedure DroppedFiles(var Message: TWMDROPFILES); message WM_DROPFILES;
    procedure LoadImages;
  public
    { Public declarations }
    property Mode: TAHMFolderMode read FMode write FMode;
    property Folder: TAHMMediaFolder read FFolder write FFolder;
    property FolderType: TAHMMediaFolderType read FType write FType;
    class function ShowFolder(Mode: TAHMFolderMode; var Folder: TAHMMediaFolder;
                              FolderType: TAHMMediaFolderType): Boolean;
  end;

var
  frmFolderDetails: TfrmFolderDetails;

implementation

{$R *.dfm}
{$WARN UNIT_PLATFORM OFF}

uses FileCtrl, ShellAPI, Utilities;

procedure TfrmFolderDetails.FormCreate(Sender: TObject);
begin
  // Create PNG image list
  FHeaderImages := TPngImageList.Create(Self);
  LoadImages;
end;

procedure TfrmFolderDetails.FormShow(Sender: TObject);
begin
  ConfigureForm;
  UpdateGUIState;

  // Accept drag & dropped folders whilst visible
  DragAcceptFiles(Handle, True);
end;

procedure TfrmFolderDetails.FormHide(Sender: TObject);
begin
  // We no longer accept drag & dropped folders whilst hidden
  DragAcceptFiles(Handle, False);
end;

procedure TfrmFolderDetails.pbHeaderPaint(Sender: TObject);
begin
  with pbHeader do
  begin
    // Draw background
    Canvas.Brush.Color := clWhite;
    Canvas.FillRect(GetClientRect);
    // Draw appropriate header image
    FHeaderImages.Draw(Canvas, 328, 0, Ord(FType));
  end;
end;

procedure TfrmFolderDetails.edDirectoryExit(Sender: TObject);
begin
  // Auto suggest folder name if missing
  if (edName.Text = '') and DirectoryExists(edDirectory.Text) then
    SuggestFolderName(LastFolder(edDirectory.Text));
end;

procedure TfrmFolderDetails.edNameChange(Sender: TObject);
begin
  UpdateGUIState;
end;

procedure TfrmFolderDetails.btBrowseClick(Sender: TObject);
var
  Dir, Name: String;
begin
  // Browse for directory
  Dir := edDirectory.Text;
  if ChooseFolder('Choose a media directory', Dir, True) then
  begin
    edDirectory.Text := Dir;

    // Auto-suggest a folder name if none assigned
    if edName.Text = '' then
    begin
      Name := ExtractFileName(Dir);
      if Name = '' then Name := GetVolumeName(Dir);
      SuggestFolderName(Name);
    end;
  end;
end;

procedure TfrmFolderDetails.btFinishClick(Sender: TObject);
begin
  // Create a new folder object in add mode
  if FMode = fmAdd then FFolder := TAHMMediaFolder.Create;

  // Update details of current folder
  FFolder.FolderName := edName.Text;
  FFolder.FolderPath := edDirectory.Text;
  FFolder.VirtualFolder := cbVirtual.Checked;
  FFolder.PersistFolder := True;
end;

procedure TfrmFolderDetails.ConfigureForm;
var
  sCaption, sContext: String;
begin
  // Display details of current/new folder
  if (FMode = fmEdit) and Assigned(FFolder) then
  begin
    edName.Text := FFolder.FolderName;
    edDirectory.Text := FFolder.FolderPath;
    cbVirtual.Checked := FFolder.VirtualFolder;
  end
  else
  begin
    edName.Text := '';
    edDirectory.Text := '';
    cbVirtual.Checked := False;
  end;

  // Customise display for mode & folder type
  case FType of
    ftMusic: sCaption := 'Music';
    ftVideo: sCaption := 'Video';
    else sCaption := 'Pictures';
  end;
  case FType of
    ftMusic: sContext := 'music';
    ftVideo: sContext := 'video';
    else sContext := 'picture';
  end;

  Caption := sCaption;
  case FMode of
    fmAdd: lbCaption.Caption := 'Add a directory to ' + sContext + ' collection'
    else lbCaption.Caption := 'Edit ' + sContext + ' directory properties';
  end;
  case FMode of
    fmAdd: lbAction.Caption := 'Select a media directory that you want to view within mediate'
    else lbAction.Caption := 'Change details for this media directory';
  end;

  // Start focus in then name edit
  edName.SetFocus;
end;

procedure TfrmFolderDetails.UpdateGUIState;
begin
  // Enable finish button if we have valid folder details
  if (Length(edName.Text) > 0) and DirectoryExists(edDirectory.Text) then
    btFinish.Enabled := True
  else
    btFinish.Enabled := False;
end;

procedure TfrmFolderDetails.SuggestFolderName(Name: String);
begin
  if Name <> '' then
  begin
    // Clean up suggested name
    Name := ExcludeTrailingPathDelimiter(Name);
    Name := Lowercase(Name);
    Name[1] := UpCase(Name[1]);
    edName.Text := Name;
  end;
end;

procedure TfrmFolderDetails.DroppedFiles(var Message: TWMDROPFILES);
var
  i: LongInt;
  Buffy: array[0..255] of char;
begin
  // Check first dropped file is a folder
  i := 0;

  if (DragQueryFile(Message.Drop, i, @Buffy, SizeOf(Buffy)) > 0) then
  begin
    if ExtractFileExt(Buffy) = '' then
      edDirectory.Text := Buffy;
  end;
end;

class function TfrmFolderDetails.ShowFolder(Mode: TAHMFolderMode; var Folder: TAHMMediaFolder;
                                            FolderType: TAHMMediaFolderType): Boolean;
var
  Form: TfrmFolderDetails;
begin
  // Set default result - failed
  Result := False;

  Form := TfrmFolderDetails.Create(Application.MainForm);
  try
    Form.PopupParent := Application.MainForm;
    Form.Mode := Mode;
    Form.Folder := Folder;
    Form.FolderType := FolderType;

    if Form.ShowModal = mrOK then Result := True;
    Folder := Form.Folder;
  finally;
    Form.Free;
  end;
end;

procedure TfrmFolderDetails.LoadImages;
begin
  with FHeaderImages do
  begin
    Clear;
    Width := 150; Height := 57;

    BeginUpdate;
    AddResourcePng('HD_IMG_MUSIC');    //0
    AddResourcePng('HD_IMG_VIDEO');    //1
    AddResourcePng('HD_IMG_PICTURE');  //2
    EndUpdate;
  end;
end;

end.
